<?php
define('API_EDUFEOL_URL', 'http://172.16.20.196/edufe_online/api/'); //东财在线接口
define('API_EDUFE_URL', 'http://218.25.161.204:7010/'); //我的教室接口  wangjun1990/helloworld
define('API_FD_URL', 'http://172.16.20.196/kcfd/api/mobile.php?'); //辅导接口
define('API_TK_URL', 'http://172.16.0.34/TKGL/jsps/tkgl/'); //题库接口

define('API_GET_LCENTER', API_EDUFEOL_URL.'lcenter.php?action=info&id=%s');
define('API_GET_LOGIN', API_EDUFE_URL.'remoteLoginMobile.action?LoginName=%s&passwd=%s');
define('API_GET_STUDENTINFO', API_EDUFE_URL.'getStudentInfoMobile.action?loginName=%s');  //查询个人信息
define('API_GET_FAREINFO', API_EDUFE_URL.'getFareInfoMobile.action?loginName=%s');  //查询费用信息
define('API_GET_HOMEWORKCOURSE', API_EDUFE_URL.'getHomeworkCourseListMobile.action?loginName=%s');  //查询作业课程列表
define('API_GET_PRACTICECOURSE', API_EDUFE_URL.'getPracticeCourseListMobile.action?loginName=%s');  //查询课程信息（综合练习）
define('API_GET_HORVERSTEHENCOURSE', API_EDUFE_URL.'getHorverstehenCourseListMobile.action?loginName=%s');  //随堂随练课程列表
define('API_GET_EXAMROOM', API_EDUFE_URL.'getExamRoomInfoMobile.action?loginName=%s');  //考场查询
define('API_GET_EXAMRESULT_MANAGE', API_EDUFE_URL.'getExamResultForManageMobile.action?loginName=%s');  //查询总部成绩信息
define('API_GET_EXAMRESULT_PERSON', API_EDUFE_URL.'getExamResultForPersonMobile.action?loginName=%s');  //查询自主成绩信息
define('API_GET_NOTICE', API_EDUFE_URL.'remoteGetNotiesInfoMobile.action?loginName=%s&syncDate=%s');  //查询成绩信息
define('API_GET_COMPOSECOURSE', API_EDUFE_URL.'getScoreComposeCourseListMobile.action?loginName=%s');  //查询成绩合成课程
define('API_GET_SCORECOMPOSE', API_EDUFE_URL.'scoreComposeMobile.action?loginName=%s&courseid=%s');  //成绩合成操作

define('API_GET_FD_BOARDS', API_FD_URL.'action=board&username=%s&parentid=%s');  //课程辅导板块列表
define('API_GET_FD_TOPICS', API_FD_URL.'action=topics&username=%s&boardid=%s&special=%s&page=%d');  //课程辅导帖子列表
define('API_GET_FD_VIEWTOPIC', API_FD_URL.'action=viewtopic&username=%s&topicid=%s&page=%s');  //课程辅导查看帖子内容
define('API_GET_FD_CANPOSTTOPIC', API_FD_URL.'action=canposttopic&username=%s&boardid=%s');  //课程辅导是否可以发帖
define('API_GET_FD_CANPOSTREPLY', API_FD_URL.'action=canpostreply&username=%s&boardid=%s');  //课程辅导是否可以回帖
define('API_GET_FD_POSTTOPIC', API_FD_URL.'action=posttopic&username=%s&boardid=%s');  //课程辅导发帖
define('API_GET_FD_POSTREPLY', API_FD_URL.'action=reply&username=%s&topicid=%s');  //课程辅导发帖

define('API_GET_TK_WORKLIST', API_TK_URL.'initWorkListMobile.action?courseValue=%s&uid=%s'); //作业列表
define('API_GET_TK_WORKANSWER', API_TK_URL.'evaluateMobile.action'); //提交作业
define('API_GET_TK_QUESTIONS', API_TK_URL.'showExampaperMobile.action?tactic_id=%s&uid=%s'); //作业题目列表
define('API_GET_STSL_TREETOP', API_TK_URL.'initTreeTopMobile.action?courseValue=%s&uid=%s'); //随堂随练章节
define('API_GET_STSL_TREELIST', API_TK_URL.'initTreeListMobile.action?courseValue=%s&chapter_id=%s&uid=%s'); //随堂随练章节
define('API_GET_STSL_QUESTIONS', API_TK_URL.'showExerExampaperMobile.action?tactic_id=%s&chapter_id=%s&uid=%s'); //随堂随练章节
define('API_GET_PRACTICELIST', API_TK_URL.'initZHListMobile.action?courseValue=%s&uid=%s');  //综合练习试题列表
define('API_GET_PRACTICEPAPER', API_TK_URL.'showZHExampaperMobile.action?tactic_id=%s&uid=%s&topic_type=%s');  //综合练习试题列表


$action = isset($_GET['a']) ? $_GET['a'] : '';
$constant = 'API_GET_'.  strtoupper($action);
if(defined($constant))
{
    unset($_GET['a']);
    $url = vsprintf(constant($constant), $_GET);
    //直接读取url
    if(empty($_POST))
    {
        switch($action):
            case 'HOMEWORKCOURSE':
                
                $json = iconv('gb2312', 'utf-8//IGNORE', @file_get_contents($url));
                if(empty($json)) {
                    raise404();
                }
                $data = json_decode($json, true);
                if(is_array($data))
                {
                    foreach($data['homeworkCourselist'] as &$item)
                    {
                        $parm = array('courseValue'=>$item['courseID'], 'username'=>$_GET['loginName']);
                        $url = vsprintf(API_GET_TK_WORKLIST, $parm);
                        $json = iconv('gb2312', 'utf-8//IGNORE', @file_get_contents($url));
                        $item['worklist'] = json_decode($json, true);
                    }
                }
                echo json_encode($data);
                break;
                
            //成绩合成列表
            case 'COMPOSECOURSE':
                
                $json = iconv('gb2312', 'utf-8//IGNORE', @file_get_contents($url));
                if(empty($json)) {
                    raise404();
                }
                $data = json_decode($json, true);
                if(is_array($data))
                {
                    foreach($data['scoreCompose'] as &$item)
                    {
                        $parm = array('courseValue'=>$item['courseID'], 'username'=>$_GET['loginName']);
                        $url = vsprintf(API_GET_TK_WORKLIST, $parm);
                        $json = iconv('gb2312', 'utf-8//IGNORE', @file_get_contents($url));
                        $item['worklist'] = json_decode($json, true);
                    }
                }
                echo json_encode($data);
                break;
            
            //随堂随练目录树
            case 'STSL_TREETOP':
                
                $json = iconv('gb2312', 'utf-8', file_get_contents($url));
                $data = json_decode($json, true);
                if(is_array($data))
                {
                    foreach($data as &$item)
                    {
                        $parm = array('courseValue'=>$_GET['courseValue'], 'chapter_id'=>$item['CHAPTER_ID'], 'username'=>$_GET['loginName']);
                        $url = vsprintf(API_GET_STSL_TREELIST, $parm);
                        $json = iconv('gb2312', 'utf-8//IGNORE', @file_get_contents($url));
                        $item['chapters'] = json_decode($json, true);
                    }
                }
                echo json_encode($data);
                break;
                
            //综合练习目录树
            case 'PRACTICECOURSE':
                
                $json = iconv('gb2312', 'utf-8//IGNORE', @file_get_contents($url));
                if(empty($json)) {
                    raise404();
                }
                $data = json_decode($json, true);
                if(is_array($data))
                {
                    foreach($data['practiceCourselist'] as &$item)
                    {
                        $parm = array('courseValue'=>$item['courseID'], 'username'=>$_GET['loginName']);
                        $url = vsprintf(API_GET_PRACTICELIST, $parm);
                        $json = iconv('gb2312', 'utf-8//IGNORE', file_get_contents($url));
                        $item['worklist'] = json_decode($json, true);
                    }
                }
                echo json_encode($data);
                break;
                
            default:
                echo iconv('gb2312', 'utf-8//IGNORE', file_get_contents($url));
                
        endswitch;
        
    }
    else //post数据
    {
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $_POST);
        curl_exec($curl);
        curl_close($curl);
    }
    exit;
}

/**
 * 发出404错误信息
 */
function raise404()
{
    header("HTTP/1.0 404 Not Found");
    exit;
}
?>
